package js.lee.jvli.task;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import js.lee.jvli.config.Configuration;
import js.lee.jvli.db.DataSourceFactory;
import js.lee.jvli.kit.EmailKit;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CheckTask implements Runnable {
    private static Logger logger = LoggerFactory.getLogger(CheckTask.class);

    @Override
    public void run() {
        logger.info("=>Check Task running...");
        Configuration config = Configuration.getInstance();
        DataSource dataSource = DataSourceFactory.getInstance().getDataSource("_mssql");
        if (dataSource == null) {
            logger.error("_mssql config not found");
            return;
        }
        QueryRunner queryRunner = new QueryRunner(dataSource);
        try {
            List<Map<String, Object>> list = queryRunner.query(
                    "select * from aUser", new MapListHandler(),
                    (Object[]) null);
            logger.debug(list.size() + "");
            if (!EmailKit.sendMail("1125482715@qq.com", "Hello", list.size() + ""))
                logger.error("Mail send failed");
            else
                logger.info("Mail sended to " + "1125482715@qq.com");
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
